package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.session.emoji.MoonUtil;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 首页日记文章 Adapter
 * Created by wjy on 2018/10/12.
 */
public class ArticleListItemAdapter extends BaseListAdapter<Article, ArticleListItemAdapter.ViewHolder> {

    private View.OnClickListener mListener;

    public ArticleListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    public void setListener(View.OnClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_article_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Article article = mData.get(position);
        if(article!=null&&article.getInvid()!=1){
            holder.content_lay.setVisibility(View.VISIBLE);
            String imageUrl = article.getImgurls();
            holder.date.setText(article.getReleaseTime());
            if (TextUtils.isEmpty(imageUrl)) {
                holder.imageLayout.setVisibility(View.GONE);
            } else {
                String images[] = imageUrl.split(",");
                holder.imageLayout.setVisibility(View.VISIBLE);
                if (images.length == 1 || article.isVideo()) {
                    holder.image1.setVisibility(View.GONE);
                    holder.image2.setVisibility(View.GONE);
                    holder.image.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(Constant.IMAGE_PREFIX + images[0]).into(holder.image);
                } else {
                    holder.image1.setVisibility(View.VISIBLE);
                    holder.image2.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(Constant.IMAGE_PREFIX + images[0]).into(holder.image1);
                    Glide.with(mContext).load(Constant.IMAGE_PREFIX + images[1]).into(holder.image2);
                    holder.image.setVisibility(View.GONE);
                }
            }
            holder.play.setVisibility(article.isVideo() ? View.VISIBLE : View.GONE);
            RequestOptions options = new RequestOptions();
            options.placeholder(R.mipmap.default_head_women);
            if(article.getUser()!=null){
                holder.name.setText(article.getUser().getNickname());
                holder.vip.setText("V" + article.getUser().getLevel());
                Glide.with(mContext).load(Constant.IMAGE_PREFIX + article.getUser().getImgurl()).apply(options).into(holder.head);

            }else {
                holder.name.setText("");
                holder.vip.setText("V" );
                holder.head.setImageResource(R.mipmap.default_head_women);
            }
            if(article.getUser()!=null&&article.getTag()!=null){
                holder.label.setText(article.getTag());

            }else {
                holder.label.setText("");
            }
            MoonUtil.identifyFaceExpression(NimUIKit.getContext(), holder.content
                    , article.getContent()
                            .replace(Constant.ARTICLE_P1,"")
                            .replace(Constant.ARTICLE_P2,"")
                            .replace(Constant.ARTICLE_TAG, "")
                    , ImageSpan.ALIGN_BOTTOM);
            holder.comments.setText(String.valueOf(article.getComment()));
            holder.views.setText(String.valueOf(article.getView()) + "人已看");
            holder.praises.setText(String.valueOf(article.getLike()));
            holder.head.setTag(R.id.position, position);

        }else {
            holder.content_lay.setVisibility(View.INVISIBLE);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, vip, date, content, label, views, praises, comments;
        LinearLayout imageLayout;
        ImageView image1, image2, image, play;
        RoundedImageView head;
        LinearLayout content_lay;
        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            content_lay = itemView.findViewById(R.id.content_lay);
            vip = itemView.findViewById(R.id.tv_vip);
            date = itemView.findViewById(R.id.tv_date);
            content = itemView.findViewById(R.id.tv_content);
            label = itemView.findViewById(R.id.tv_label);
            views = itemView.findViewById(R.id.tv_view);
            praises = itemView.findViewById(R.id.tv_praise);
            comments = itemView.findViewById(R.id.tv_comment);
            head = itemView.findViewById(R.id.iv_head);
            imageLayout = itemView.findViewById(R.id.ll_image_layout);
            image1 = itemView.findViewById(R.id.iv_image_1);
            int itemWidth = (Constant.SCREEN_WIDTH - Utils.dpToPx(74, mContext.getResources())) / 2;
            ViewGroup.LayoutParams layoutParams = image1.getLayoutParams();
            layoutParams.width = itemWidth;
            layoutParams.height = itemWidth;
            image1.setLayoutParams(layoutParams);
            image2 = itemView.findViewById(R.id.iv_image_2);
            image = itemView.findViewById(R.id.iv_image);
            ViewGroup.LayoutParams layoutParams1 = image2.getLayoutParams();
            layoutParams1.width = itemWidth;
            layoutParams1.height = itemWidth;
            image2.setLayoutParams(layoutParams1);
            ViewGroup.LayoutParams layoutParams2 = image.getLayoutParams();
            layoutParams2.width = Constant.SCREEN_WIDTH - Utils.dpToPx(64, mContext.getResources());
            layoutParams2.height = itemWidth;
            image.setLayoutParams(layoutParams2);
            play = itemView.findViewById(R.id.iv_play);
            head.setOnClickListener(mListener);
        }
    }


}
